<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/8/29
 * Time: 21:06
 */

namespace app\cms\controller;

use app\common\controller\AdminBase;
use app\common\model\Form as FormModel;
use app\common\model\FormField as FormFieldModel;
use app\cms\model\Cms as CmsModel;
use think\App;
use think\facade\Request;
use app\common\model\FormHelper;


class Admin extends AdminBase
{
    protected $formid = '788CFE8C203F7D1036AE82F2EE7C61EC';

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $helper = new FormHelper();
        $this->assign('Helper',$helper);
    }

    public function listing()
    {
        $page = Request::param('page/d');
        $pagesize = Request::param('limit/d');
        $fields = FormFieldModel::instance()->getFields($this->formid);

        if(Request::isAjax()){
            $res = FormModel::instance()->getPageDataList($this->formid,'','',$page,$pagesize);
            $list = $res['list'];
            foreach($list as $k=>$v){
                $model = array();
                foreach($v as $kk=>$vv){
                    if($kk == '_guid')
                        continue;
                    $model[$kk] = FormHelper::instance()->parse($fields[$kk],$v);
                }
                $list[$k] = $model;
            }
            $res['list'] = $list;

            $this->success('数据列表','',$res);
            if($res === true){
                $this->success('成功');
            }else{
                $this->error($res);
            }
        }else{
            $this->assign('formid',$this->formid);
            $this->assign('fields',$fields);
            return $this->fetch();
        }
    }

    public function add(){
        if(Request::isPost()){
            $form = Request::param('form/a');
            $res = FormModel::instance()->addFormData($this->formid,$form);
            if($res === true){
                $this->success('成功',url('listing'));
            }else{
                $this->error($res);
            }
        }else{
            $model = FormModel::instance()->getFormData($this->formid,'');
            $fields = FormFieldModel::instance()->getFields($this->formid);
            $this->assign('fieldtypes',FormFieldModel::instance()->type);
            $this->assign('fields',$fields);
            $this->assign('formid',$this->formid);
            $this->assign('model',$model);
            return $this->fetch('model');
        }
    }

    public function edit(){
        $guid = Request::param('guid/s');
        if(Request::isPost()){
            $form = Request::param('form/a');
            $res = FormModel::instance()->editFormData($this->formid,$guid,$form);
            if($res === true){
                $this->success('成功',url('listing'));
            }else{
                $this->error($res);
            }
        }else{
            $model = FormModel::instance()->getFormData($this->formid,$guid);
            $fields = FormFieldModel::instance()->getFields($this->formid);
            $this->assign('fieldtypes',FormFieldModel::instance()->type);
            $this->assign('fields',$fields);
            $this->assign('formid',$this->formid);
            $this->assign('model',$model);
            return $this->fetch('model');
        }
    }


    public function remove(){
        $guid = Request::param('guid/s');
        $res = FormModel::instance()->removeFormData($this->formid,$guid);
        if($res === true){
            $this->success('成功',url('listing'));
        }else{
            $this->error($res);
        }
    }
    public function test(){
        $Render = new FormDisplay();
        $model = FormModel::instance()->getFormData($this->formid,'');
        $fields = FormFieldModel::instance()->getFields($this->formid);
        $this->assign('model',$model);
        $this->assign('fields',$fields);
        $this->assign('Render',$Render);
        return $this->fetch();
    }
}